/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-07-28 14:44:57
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-07-29 15:27:49
 * @FilePath: /yyshort/lib/pages/search_result/view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';
import 'package:yyshort/common/model/recommend_list_model.dart';
import 'package:yyshort/common/model/video_model.dart';
import 'package:yyshort/pages/ali_player/short_video/short_video_page.dart';
import 'package:yyshort/pages/search_result/widgets/hello.dart';

import 'index.dart';

class SearchResultPage extends GetView<SearchResultController> {
  const SearchResultPage({super.key});

  @override
  Widget build(BuildContext context) => KeyboardDismisser(
    gestures: [GestureType.onTap, GestureType.onPanUpdateDownDirection],
    child: GetBuilder<SearchResultController>(
      builder: (_) {
        // 监听输入变化，实时更新UI
        controller.searchController.addListener(() {
          controller.update();
        });
        return Scaffold(
          appBar: AppBar(
            // 替换原来的 title 为自定义搜索栏
            titleSpacing: 0, // 消除标题与左侧返回按钮的默认间距
            leadingWidth: 40, // 减小返回按钮的宽度空间
            title: _buildSearchBar(),
            // elevation: 1,
          ),
          body: SafeArea(
            child: SearchResultWidget(
              onPlayTap: (video) {
                print('点击视频标题: ${video.name}');
                Get.to(
                  () => ShortVideoPage(
                    preload: false,
                    isforyouPage: false,
                    superModel: video,
                  ),
                );
              },
            ),
          ),
        );
      },
    ),
  );

  // 构建自定义搜索栏
  Widget _buildSearchBar() {
    return Container(
      height: 34,
      margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
      decoration: BoxDecoration(
        color: Color(0xFF1C1C1C),
        borderRadius: BorderRadius.circular(17),
        boxShadow: [BoxShadow(color: Colors.black54, blurRadius: 0.5)],
      ),
      child: Row(
        children: [
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: Icon(Icons.search, color: Colors.grey, size: 20),
          ),
          Expanded(
            child: TextField(
              controller: controller.searchController,
              focusNode: controller.focusNode,
              decoration:  InputDecoration(
                hintText: '${'请输入搜索内容'.tr}...',
                hintStyle: TextStyle(color: Color(0xFF919191)),
                border: InputBorder.none,

                isDense: true,
                contentPadding: EdgeInsets.zero,
              ),
              style: const TextStyle(fontSize: 16, color: Colors.white),
              onTap: () {
                // 点击时获取焦点，弹出键盘
                controller.focusNode.requestFocus();
              },
              onSubmitted: (value) {
                // 提交搜索
                controller.submitSearch(value);
              },
            ),
          ),
          // 只有当有输入内容时才显示清空按钮
          if (controller.searchController.text.isNotEmpty)
            IconButton(
              icon: const Icon(Icons.clear, color: Colors.grey, size: 20),
              onPressed: () {
                controller.clearSearchText();
              },
              padding: const EdgeInsets.symmetric(horizontal: 8),
              constraints: const BoxConstraints(),
            ),
          const SizedBox(width: 8),
        ],
      ),
    );
  }
}
